package com.guoliang.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guoliang.eduService.entity.EduVideo;
import com.guoliang.eduService.mapper.EduVideoMapper;
import com.guoliang.eduService.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guoliang.eduService.service.VodClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author leo
 * @since 2023-01-18
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    @Autowired
    private VodClient vodClient;
    @Override
    public void removeVideoByCourseId(String id) {
        //根据课程id查询视频id
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.select("video_source_id");
        wrapper.eq("course_id",id);
        List<EduVideo> eduVideos = baseMapper.selectList(wrapper);
        List<String> videoIds=new ArrayList<>();
        for (EduVideo eduVideo : eduVideos) {
            String videoSourceId = eduVideo.getVideoSourceId();
            if (!StringUtils.isEmpty(videoSourceId))  videoIds.add(videoSourceId);
        }
        if (videoIds.size()!=0)  vodClient.deleteBatch(videoIds);
        //删除video
        QueryWrapper<EduVideo> eduVideoQueryWrapper=new QueryWrapper<>();
        eduVideoQueryWrapper.eq("course_id",id);
        baseMapper.delete(eduVideoQueryWrapper);
    }
}
